package com.liangtian.mdm.dm.listener;

import com.liangtian.mdm.dm.model.model.Model;
import com.liangtian.mdm.system.user.model.SysUser;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;

import java.util.ArrayList;
import java.util.HashMap;

@SuppressWarnings({"rawtypes"})
public class Ehcache {

    private Cache<String, SysUser> userCache = null;

    // modelId：model
    private Cache<String, Model> modelCache = null;

    // modelId：数据库列
    private Cache<String, String> columnsCache = null;

    // modelId：Map<column:AttrConfig>
    private Cache<String, HashMap> attrConfigCache = null;

    // modelId：groupList 屬性組不常用，易變化，暫不緩存
    // static Cache<String, ArrayList> groupsCache = null;

    // modelId：转义列
    private static Cache<String, String> queryColsCache = null;

    //processDefineId:UserTask
    private Cache<String, ArrayList> wfTaskCache = null;

    // singleton
    private static Ehcache cache = new Ehcache();

    private Ehcache() {
        //缓存管理器
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
        cacheManager.init();

        userCache = cacheManager.createCache("userCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, SysUser.class, ResourcePoolsBuilder.heap(200)).build());
        modelCache = cacheManager.createCache("modelCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, Model.class, ResourcePoolsBuilder.heap(200)).build());
        columnsCache = cacheManager.createCache("columnsCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(200)).build());
        attrConfigCache = cacheManager.createCache("attrConfigCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, HashMap.class, ResourcePoolsBuilder.heap(200)).build());
        queryColsCache = cacheManager.createCache("queryColsCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(200)).build());
        wfTaskCache = cacheManager.createCache("wfTaskCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, ArrayList.class, ResourcePoolsBuilder.heap(200)).build());
    }

    public static Ehcache getCache() {
        return cache;
    }

    public Cache<String, SysUser> getUserCache() {
        return userCache;
    }

    public Cache<String, Model> getModelCache() {
        return modelCache;
    }

    public Cache<String, String> getColumnsCache() {
        return columnsCache;
    }

    public Cache<String, HashMap> getAttrConfigCache() {
        return attrConfigCache;
    }

    public Cache<String, String> getQueryColsCache() {
        return queryColsCache;
    }

    public Cache<String, ArrayList> getWfTaskCache() {
        return wfTaskCache;
    }

}
